package com.lanyuan.a.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.bean.union.Profit;
import com.lanyuan.a.dao.BaseDao;
import com.lanyuan.entity.base.FormMap;
import com.lanyuan.plugin.PageView;

@Transactional
@Repository("baseDao")
@SuppressWarnings("all")
public class BaseDaOImpl<T> implements BaseDao<T> {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	public synchronized void update(T o) {
		this.getCurrentSession().update(o);
	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}

	public List<T> find(String hql) {
		return this.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<T> findSql(String sql) {
		return this.getCurrentSession().createSQLQuery(sql).list();
	}
	@Override
	public List<T> findSql(String sql, Object[] param) {
		Query q = this.getCurrentSession().createSQLQuery(sql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}
	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}

	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.list();
	}

	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}
	
	@Override
	public List<T> findSql(String sql, Object[] param, Integer page,
			Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createSQLQuery(sql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		List list = q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
		return list;
	}

	public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}
	@Override
	public int countSql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createSQLQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list().size();
	}

	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}

	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}

	@Override
	public int getCountByCondition(String hql, Object[] param) {
	    Query q = this.getCurrentSession().createQuery(hql);
	    if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list().size();
	}


	public List<T> queryByPage1(String hql, Object[] param, FormMap<?, ?> formMap) {
		Query q ;
		Long count;
	   String countHql = "";
		String ss = (String)formMap.get("accountname");
		if(ss != null && !ss.equals("")){
			hql = "from User u where accountname like :accountname";
			countHql = "select count (1) from User u where accountname like ?";
			count = getCountByConditionLike(countHql , new Object[]{ss});
			q = this.getCurrentSession().createQuery(hql);
			q.setString("accountname", "%" + ss + "%");
		}else{
			countHql = "select count (1) from User u ";
			count = count(countHql , new Object[]{});
			q = this.getCurrentSession().createQuery(hql);
			 if (param != null && param.length > 0) {
					for (int i = 0; i < param.length; i++) {
						q.setParameter(i, param[i]);
					}
				}
		}
	    PageView pageView = (PageView)formMap.get("paging");
	    q.setFirstResult(pageView.getFirstResult());  
	    q.setMaxResults(pageView.getPageSize());
	    pageView.setRowCount(count);
		return q.list();
	}
	
	
	@Override
	public Long getCountByConditionLike(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, "%"+param[i]+"%");
			}
		}
		return (Long) q.uniqueResult();
	}

	@Override
	public void delete(String hql, String pm , Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		q.setParameterList(pm, param);
		int i = q.executeUpdate();
		System.out.println("删除的条数"+i);
	}
	
	public void test(){
		String hql = "   select new Profit(w_name , c_name , totalprofit , saletotal) from (select ss.w_name , ss.c_name,ss.totalprofit, sum(ss.totalprofit) as saletotal from ( select ws.* , st.profit ,(ws.total*st.profit) as totalprofit from (select s.w_f_id , (s.price - s.cost) as profit from Store s) st , (select c_id ,c_name , w_id ,w_name,w_f_id ,sum(f_salenum) as total  from WindowSale where saledate > '2016-03-04 20:21:22' and w_id in (select w_id from Window ) GROUP BY w_id, w_f_id ) ws where st.w_f_id = ws.w_f_id ) ss group by ss.w_id)";
		Query q = this.getCurrentSession().createQuery(hql);
//		List<Profit> list = q.a
//		for(Profit p : list){
//			System.out.println(p);
//		}
	}

	
	@Override
	public List<T> selectIn(String hql, String pm , Object[] param , Integer page, Integer rows ) {
		Query q = this.getCurrentSession().createQuery(hql);
		q.setParameterList(pm, param);
		return q.list();
//		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	@Override
	public int countIn(String hql, String pm, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		q.setParameterList(pm, param);
		return q.list().size();
	}
}
